package com.nobody.facepk.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nobody.facepk.model.Picture;
import com.nobody.facepk.model.User;
import com.nobody.facepk.model.UserData;
import com.nobody.facepk.model.UserDataType;
import com.nobody.facepk.pojo.UserDataHelper;
import com.nobody.facepk.pojo.json.Types;
import com.nobody.facepk.sns.Event;
import com.nobody.facepk.sns.EventType;
import com.trg.dao.hibernate.GeneralDAO;

@Service
public class EventService {
	private GeneralDAO generalDao;
	public void handleEvent(Event event) {
		UserData userData = UserDataHelper.getUserData(event.getUserId(), UserDataType.RECENT_EVENTS);
		if(userData==null) {	
			userData = new UserData(event.getUserId(), UserDataType.RECENT_EVENTS);
			List<Event> events = new ArrayList<Event>();
			events.add(event);
			userData.setData(events);
		} else {
			List<Event> events = userData.getDataAsList(Event.LIST_TYPE);
			events.add(event);
			if(events.size()>10) events.remove(0);
			userData.setData(events);
		}
		this.generalDao.save(userData);
//		if(event.getType()==EventType.SCORING) {
//			this.handleScoringEvent(event);
//		}
	}
		
	@Transactional(readOnly=false)
	public List getUserEvent(User user){
		List<Event> events = new ArrayList<Event>();
		UserData userData = UserDataHelper.getUserData(user.getId(), UserDataType.RECENT_EVENTS);
		if(userData != null){
			events = userData.getDataAsList(Event.LIST_TYPE);
		}
		return events;
	}
	
	private void handleScoringEvent(Event event) {
		//更新打分人最近打分记录
	}
	
	@Autowired
	public void setGeneralDao(GeneralDAO generalDao) {
		this.generalDao = generalDao;
	}
}
